/*
@ClassName PlanScheduleController
@Description TODO
@Author SubUp
@Date   2022/7/31
@Version 1.0
*/

package com.sunjob.mrojobschedule.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.sunjob.common.domain.loginServer.SysUser;
import com.sunjob.common.domain.scheduleServer.PlanSchedule;
import com.sunjob.common.domain.scheduleServer.orm.PlanResult;
import com.sunjob.common.exception.MROException;
import com.sunjob.common.mroAnno.ControllerLog;
import com.sunjob.common.mroAnno.LogModuleType;
import com.sunjob.common.result.Result;
import com.sunjob.mrojobschedule.service.PlanScheduleService;
import com.sunjob.mrojobschedule.service.ProductScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@Api(tags = "计划调度",value = "处理计划管理相关请求")
@RequestMapping("/plan/")
//检验登录
//@SaCheckLogin
public class PlanScheduleController {
    @Resource
    private PlanScheduleService planScheduleService;

    @GetMapping("list/{pageId}")
    @ControllerLog(LogModuleType.PLAN)
    public Result<List> showPlan(@PathVariable String pageId){
        List<PlanSchedule> planSchedules = planScheduleService.selectPlanList(pageId);
        if(planSchedules.size()>0){
            return Result.ok(planSchedules,"plan message success");
        }
        return Result.build(null,200,"plan message failure");
    }

    @GetMapping("list/orm/{pageId}")
    @ApiOperation(value = "获取所有计划信息")
    @ControllerLog(LogModuleType.PLAN)
    public Result<List<PlanResult>> showPlanOrm(@ApiParam(value = "分页编号",defaultValue = "1") @PathVariable String pageId){
        List<PlanResult> planResults = planScheduleService.selectPlanOrmList(pageId);
        if(planResults.size()>0){
            return Result.ok(planResults,"plan message success");
        }
        return Result.build(null,200,"plan message failure");
    }

    @GetMapping("query/prim/{planId}/{pageId}")
//    @ApiOperation(value = "查询planId对应的原始计划信息")
    @ControllerLog(LogModuleType.PLAN)
    public Result<List<PlanSchedule>> showPlanById(@PathVariable String planId,
                                                   @PathVariable String pageId){
        List<PlanSchedule> planScheduleList = planScheduleService.selectPlanById(planId);
        if(planScheduleList.size()>0){
            return Result.ok(planScheduleList,"search primitive plan message success by planId");
        }
        return Result.build(null,200,"search primitive plan message failure by planId");
    }
    @GetMapping("query/number/{planId}/{pageId}")
    @ApiOperation(value = "查询planId对应的计划信息")
    @ControllerLog(LogModuleType.PLAN)
    public Result<List<PlanResult>> showPlanOrmById(@ApiParam(value = "计划编号",required = true) @PathVariable String planId,
                                                    @ApiParam(value = "分页编号",defaultValue = "1") @PathVariable String pageId){
        List<PlanResult> planResults = planScheduleService.selectPlanOrmById(planId);
        if(planResults.size()>0){
            return Result.ok(planResults,"search processing plan message success by planId");
        }
        return Result.build(null,200,"search processing plan message failure by planId");
    }

    @GetMapping("query/orm/name/{objName}/{pageId}")
    @ControllerLog(LogModuleType.PLAN)
    @ApiOperation(value = "检修对象名称查询计划信息")
    public Result<List<PlanResult>> showPlanOemByName(@ApiParam(value = "检修名称",required = true) @PathVariable String objName,
                                                @ApiParam(value = "分页编号",defaultValue = "1") @PathVariable String pageId){
        List<PlanResult> planResults = planScheduleService.selectPlanResultByObjName(objName);
        if(planResults.size()>0)
            return Result.ok(planResults,"search processing plan message success by planId");
        return Result.build(null,200,"search processing plan message failure by planId");
    }

    @PostMapping("save/{planObj}")
    @ControllerLog(LogModuleType.PLAN)
    //校验角色
//    @SaCheckRole(value = "common")
    @ApiOperation(value = "添加计划记录")
    public Result<Integer> addPlan(@ApiParam(value = "计划信息",required = true) @RequestBody PlanSchedule planObj){
        Integer rt = planScheduleService.addSave(planObj);
        if(rt >0){
            return Result.ok(rt,"insert plan success");
        }
        return Result.build(null,200,"insert plan failure");
    }

    @PostMapping("edit/{planObj}")
    @ControllerLog(LogModuleType.PLAN)
    @ApiOperation(value = "编辑、修改计划数据")
//    @ApiImplicitParam(value = "planId",name = "planId",paramType = "query",required = true,dataType = "long")
    public Result<Integer> editPlan(@ApiParam(value = "计划信息",required = true) @RequestBody PlanSchedule planObj){
        Integer rt = planScheduleService.editSave(planObj);
        if(rt != 0){
            return Result.ok(rt,"edit primitive plan success!");
        }
        return Result.build(rt,200,"edit primitive plan failure !");
    }
    @PostMapping("edit/orm/{planObj}")
    @ControllerLog(LogModuleType.PLAN)
    @ApiOperation(value = "编辑、修改计划数据")
//    @ApiImplicitParam(value = "planId",name = "planId",paramType = "query",required = true,dataType = "long")
    public Result<Integer> editPlanResult(@ApiParam(value = "计划信息",required = true) @RequestBody PlanResult planObj){
        Integer rt = planScheduleService.editPlanSave(planObj);
        if(rt != 0){
            return Result.ok(rt,"edit plan success!");
        }
        return Result.build(rt,200,"edit plan failure !");
    }
    @PostMapping("edit/examinestate/{planId}/{examineState}")
    @ControllerLog(LogModuleType.PLAN)
    @ApiOperation(value = "修改计划的审批状态")
    public Result<Integer> editExamineStateById(@ApiParam(value = "计划编号",required = true) @PathVariable String planId,
                                                @ApiParam(value = "审批状态",required = true) @PathVariable String examineState){
        Integer rt = planScheduleService.updateSaveByExamineState(planId,
                examineState);
        if(rt != 0){
            return Result.ok(rt,"edit  plan examine state success!");
        }
        return Result.build(rt,200,"edit  plan examine state failure !");
    }

    @PostMapping("edit/planstate/{planId}/{planState}")
    @ControllerLog(LogModuleType.PLAN)
    @ApiOperation(value = "修改计划状态")
    public Result<Integer> editPlanStateById(@ApiParam(value = "计划编号",required = true) @PathVariable String planId,
                                                @ApiParam(value = "审批状态",required = true) @PathVariable String planState){
        Integer rt = planScheduleService.updateSaveByPlanState(planId,
                planState);
        if(rt != 0){
            return Result.ok(rt,"edit  plan plan state success!");
        }
        return Result.build(rt,200,"edit  plan plan state failure !");
    }

    @GetMapping("remove/{planId}")
    @ControllerLog(LogModuleType.PLAN)
    @ApiOperation(value = "删除计划数据")
//    @ApiImplicitParam(value = "planId",name = "planId",paramType = "query",required = true,dataType = "long")
    public Result<Integer> removePlan(@ApiParam(value = "计划编号",required = true) @PathVariable String planId){
//        SaSession tokenSession = StpUtil.getTokenSession();
//        SysUser userInfo = (SysUser)tokenSession.get("userInfo");
        Integer rt = planScheduleService.deleteSave(planId);
        if(rt != 0){
            return Result.ok(rt,"remove plan success!");
        }
        return Result.build(rt,200,"remove plan failure !");
    }

    @GetMapping("remove/{planId}/{examineState}")
    @ControllerLog(LogModuleType.PLAN)
    public Result<Integer> removePlanByExamineState(@PathVariable String planId,
                                                    @PathVariable String examineState){
        Integer rt = planScheduleService.deleteSaveByExamineState(planId, examineState);
        if(rt != 0)
            return Result.ok(rt,"remove plan success!");
        return Result.build(rt,200,"remove plan failure !");
    }

    @GetMapping("nature/{name}/{planStateExplain}")
    @ResponseBody
    @ApiOperation(value = "计划性质")
    @ControllerLog(LogModuleType.PLAN)
    public Result<Map> planNature(@PathVariable String staffName,@PathVariable Integer planStateExplain)
    {
        Map map = planScheduleService.planNature(staffName,planStateExplain);
        if (map.size()>0) {
            return Result.ok(map,"yes");
        }
        return Result.fail("no");
    }
    @GetMapping("query/nature/{natureId}")
    @ApiOperation(value = "计划性质分类返回计划数据")
    @ControllerLog(LogModuleType.PLAN)
    public Result<List<PlanResult>> selectNaturePlanById(@ApiParam(value = "计划性质编号",defaultValue = "0") @PathVariable String natureId){
        List<PlanResult> planResults = planScheduleService.selectPlanResultByNatureId(natureId);
        if(planResults.size() != 0)
            return Result.ok(planResults,"message is ok");
        return Result.build(null,200,"no message");
    }


}
